package hfuu.com.cn.teasystemapp;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.style.BulletSpan;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import org.json.JSONObject;

import java.io.IOException;
import java.io.Serializable;
import java.nio.BufferUnderflowException;
import java.text.BreakIterator;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import hfuu.com.cn.teasystemapp.bean.farmmedcine;
import hfuu.com.cn.teasystemapp.bean.farmtrim;
import hfuu.com.cn.teasystemapp.bean.land;
import hfuu.com.cn.teasystemapp.bean.role;
import hfuu.com.cn.teasystemapp.bean.user;
import hfuu.com.cn.teasystemapp.util.Bean2Map;
import hfuu.com.cn.teasystemapp.util.HttpUtils;
import hfuu.com.cn.teasystemapp.util.listviewutil;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class landactivty extends Activity {
    private SimpleAdapter adapter;
    private String res = "";
    private List<HashMap<String, Object>> userdata = new ArrayList<HashMap<String, Object>>();
    private List<HashMap<String, Object>> landstatedate = new ArrayList<HashMap<String, Object>>();
    farmmedcine fm = new farmmedcine();
    farmtrim ft = new farmtrim();
    AlertDialog dialog1;
    AlertDialog dialogm;
    AlertDialog dialogtrim;
    List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
    List userlist1 = new ArrayList();
    land l = new land();
    private String land;

    private String landname = "";
    List<user> userList = new ArrayList<user>();
    int sw = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        sw = 0;
        land = "";
        data = new ArrayList<HashMap<String, Object>>();
        userdata = new ArrayList<HashMap<String, Object>>();
        landstatedate = new ArrayList<HashMap<String, Object>>();
        userlist1 = new ArrayList();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_land);
        List<land> landlist = new ArrayList<land>();
        final Intent intent = getIntent();
        landlist = (List<land>) intent.getSerializableExtra("landlist");

        getuselist();
        //Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        final ListView listView = (ListView) this.findViewById(R.id.landlistview);
        for (land l : landlist) {
            HashMap<String, Object> item = new HashMap<String, Object>();
            item.put("id", l.getLandId());
            item.put("landname", l.getLandName());
            l.setLandSite(l.getLandSite().replace("安徽省", ""));
            item.put("landsite", l.getLandSite());
            item.put("landvarite", l.getLandVariety());
            if (l.getLandState() == 0) {
                item.put("landstate", "关闭");
            } else {
                item.put("landstate", "正常");
            }
            item.put("isfocus", false);
            item.put("landarea", l.getLandArea());
            item.put("landuser", l.getLandFunctionary());
            item.put("landtime", l.getLandTime());
            item.put("landid", l.getLandId());

            data.add(item);
        }

        Log.i("land", data.toString());
        adapter = new SimpleAdapter(this, data, R.layout.item,
                new String[]{"landname", "landvarite", "landstate"}, new int[]{R.id.landname, R.id.landvarite, R.id.landstate}) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = super.getView(position, convertView, parent);
                Boolean isFocus = (Boolean) data.get(position).get("isfocus");

                if (isFocus) {
                    view.setBackgroundColor(getResources().getColor(R.color.tv_gray));
                } else {
                    view.setBackgroundColor(Color.WHITE);
                }
                return view;
            }

        };

        listView.setAdapter(adapter);

        listviewutil.setListViewHeightBasedOnChildren(listView);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, final View view, final int position, long id) {
                sw = 0;
                Log.i("c", "" + view.getSolidColor());
                HashMap<String, String> h1 = (HashMap<String, String>) parent.getItemAtPosition(position);
                data.get(position).put("isfocus", true);
                adapter.getView(position, view, parent);
                for (int i = 0; i < data.size(); i++) {
                    if (i != position) {
                        data.get(i).put("isfocus", false);
                        //  Log.i("i", "ssssssss" + i);
                        adapter.getView(i, view, parent);
                    }
                }
                adapter.notifyDataSetChanged();

                // Toast.makeText(landactivty.this, h1.get("landname"), Toast.LENGTH_LONG).show();
                AlertDialog dialog = new AlertDialog.Builder(landactivty.this).setTitle("请选择操作").setSingleChoiceItems(new String[]{"修改", "施肥、农药", "除草、修剪"}, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        sw = 0;
                        sw = which;
                    }
                }).setNeutralButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(final DialogInterface dialog, int which) {
                        Log.i("dialog", "" + dialog + "," + which + "ssss" + sw);
                        Log.i("land", data.get(position).toString());
                        if (sw == 0) {//修改

                            builder = new AlertDialog.Builder(landactivty.this);
                            builder.setTitle("修改数据");


                            dialoginit();

                            final String landid = data.get(position).get("landid").toString();


                            // data.get(position).get("");
                            EditText landname1 = (EditText) view1.findViewById(R.id.landname);
                            landname1.setText(data.get(position).get("landname").toString());
                            ((EditText) view1.findViewById(R.id.landarea)).setText(data.get(position).get("landarea").toString());
                            ((EditText) view1.findViewById(R.id.landsite)).setText(data.get(position).get("landsite").toString());
                            ((EditText) view1.findViewById(R.id.landvarite)).setText(data.get(position).get("landvarite").toString());
                            ((EditText) view1.findViewById(R.id.landtime)).setText(data.get(position).get("landtime").toString());

                            Spinner s = (Spinner) view1.findViewById(R.id.statelist);
                            Spinner s1 = (Spinner) view1.findViewById(R.id.userlist);

                            SpinnerAdapter sa = s.getAdapter();
                            for (int i = 0; i < sa.getCount(); i++) {
                                HashMap<String, Object> map = new HashMap<String, Object>();
                                map = (HashMap<String, Object>) sa.getItem(i);
                                if (data.get(position).get("landstate").toString().equals(map.get("landstatename"))) {
                                    s.setSelection(i);
                                }
                            }
                            SpinnerAdapter sa1 = s1.getAdapter();
                            Log.i("size1", "" + sa1.getCount());
                            Log.i("size2", position + "");
                            Log.i("userlist", userList.toString());
                            Log.i("userlist", userdata.toString());
                            for (int i = 0; i < sa1.getCount(); i++) {
                                HashMap<String, Object> map = new HashMap<String, Object>();
                                map = (HashMap<String, Object>) sa1.getItem(i);
                                //Log.i("testui", data.get(position).get("landuser").toString() + "==" + map.toString());

                                if (data.get(position).get("landuser").toString().equals(map.get("userid"))) {

                                    s1.setSelection(i);
                                }
                            }

                            Button buttonok = (Button) view1.findViewById(R.id.landaddok);
                            Button buttoncancel = (Button) view1.findViewById(R.id.landaddcancel);
                            buttonok.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    boolean b = getviewdata();
                                    if (!b) {
                                        return;
                                    } else {
                                        l.setLandId(landid);
                                        Gson gson = new Gson();
                                        land = gson.toJson(l);
                                        Log.i("land", land.toString());
                                        Thread thread = new Thread(new Runnable() {
                                            @Override
                                            public void run() {
                                                try {
                                                    res = HttpUtils.post("land/editprocad", land);
                                                    Message msg = new Message();
                                                    msg.what = 1;
                                                    uiHandler.sendMessage(msg);
                                                } catch (IOException e) {
                                                    e.printStackTrace();
                                                }
                                            }
                                        });
                                        thread.start();


                                    }
                                }
                            });
                            buttoncancel.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    dialog1.cancel();
                                    landactivty.this.onCreate(null);
                                    return;
                                    // Toast.makeText(landactivty.this,"cancael", Toast.LENGTH_SHORT).show();

                                }
                            });

                        } else if (sw == 1) {//施肥、农药
                            View view2 = LayoutInflater.from(landactivty.this).inflate(R.layout.landmedcine, null);
                            AlertDialog.Builder medcinebuilder = new AlertDialog.Builder(landactivty.this);
                            Spinner types = (Spinner) view2.findViewById(R.id.medcinelist);
                            Spinner operates = (Spinner) view2.findViewById(R.id.medcineoperatelist);
                            dialogm = medcinebuilder.create();
                            types.setDropDownVerticalOffset(30);
                            operates.setDropDownVerticalOffset(30);
                            dialogm.setView(view2);
                            dialogm.show();
                            WindowManager window = getWindowManager();
                            Display display = window.getDefaultDisplay();
                            int screenheight = display.getHeight();
                            int screenWidth = display.getWidth();


                            //设置弹出框的长宽
                            dialogm.getWindow().setLayout(screenWidth - 10, screenheight / 2);
                            types.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                @Override
                                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                    String m = (String) parent.getSelectedItem();
                                    if (m.equals("修剪")) {
                                        fm.setFarmType(0);
                                    } else if (m.equals("除草")) {
                                        fm.setFarmType(1);
                                    }
                                }

                                @Override
                                public void onNothingSelected(AdapterView<?> parent) {

                                }
                            });

                            operates.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                @Override
                                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                    String m = (String) parent.getSelectedItem();
                                    if (m.equals("灌溉")) {
                                        fm.setFarmOperate(0);
                                    } else if (m.equals("喷雾")) {
                                        fm.setFarmOperate(1);
                                    } else if (m.equals("滴注")) {
                                        fm.setFarmOperate(2);
                                    } else if (m.equals("其他")) {
                                        fm.setFarmOperate(3);
                                    }
                                }

                                @Override
                                public void onNothingSelected(AdapterView<?> parent) {

                                }
                            });
                            String medcinename = ((EditText) view2.findViewById(R.id.medcinename)).getText().toString().trim();
                            String medcinecount = ((EditText) view2.findViewById(R.id.medcinecount)).getText().toString().trim();
                            fm.setFarmMedcine(medcinename);
                            fm.setFarmMedcineamount(medcinecount);
                            String landid = data.get(position).get("landid").toString();
                            fm.setFarmLandid(landid);
                            Button buttonok = (Button) view2.findViewById(R.id.landaddok);
                            Button buttoncancel = (Button) view2.findViewById(R.id.landaddcancel);
                            Gson gson = new Gson();
                            final String medcine = gson.toJson(fm);
                            buttonok.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {

                                    Thread thread = new Thread(new Runnable() {
                                        @Override
                                        public void run() {
                                            HttpUtils.doPost("medcine/addprocad", medcine, new Callback() {
                                                @Override
                                                public void onFailure(Call call, IOException e) {
                                                    Looper.prepare();
                                                    Toast.makeText(landactivty.this, "网络连接失败", Toast.LENGTH_LONG).show();
                                                    Looper.loop();
                                                }

                                                @Override
                                                public void onResponse(Call call, Response response) throws IOException {
                                                    res = response.body().string();
                                                    Message msg = new Message();
                                                    msg.what = 3;
                                                    uiHandler.sendMessage(msg);
                                                }
                                            });
                                        }
                                    });
                                    thread.start();


                                }
                            });
                            buttoncancel.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    onCreate(null);
                                    dialogm.cancel();
                                    return;
                                }
                            });
                        } else if (sw == 2) {//除草
                            View view3 = LayoutInflater.from(landactivty.this).inflate(R.layout.landtrim, null);

                            AlertDialog.Builder trimbuilder = new AlertDialog.Builder(landactivty.this);
                            Spinner trim = (Spinner) view3.findViewById(R.id.trimlist);
                            dialogtrim = trimbuilder.create();
                            trim.setDropDownVerticalOffset(30);
                            dialogtrim.setView(view3);
                            dialogtrim.show();
                            WindowManager window = getWindowManager();
                            Display display = window.getDefaultDisplay();
                            int screenheight = display.getHeight();
                            int screenWidth = display.getWidth();
                            String landid = data.get(position).get("landid").toString();
                            ft.setFarmLandid(landid);
                            //设置弹出框的长宽
                            dialogtrim.getWindow().setLayout(screenWidth - 10, screenheight / 2);
                            trim.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                @Override
                                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                    String m = (String) parent.getSelectedItem();
                                    if (m.equals("修剪")) {
                                        ft.setFarmType(0);
                                    } else if (m.equals("除草")) {
                                        ft.setFarmType(1);
                                    }
                                }

                                @Override
                                public void onNothingSelected(AdapterView<?> parent) {

                                }
                            });
                            Gson gson = new Gson();
                            final String trimj = gson.toJson(ft);
                            Button buttonok = (Button) view3.findViewById(R.id.landaddok);
                            Button buttoncacel = (Button) view3.findViewById(R.id.landaddcancel);
                            buttonok.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {

                                    Thread thread = new Thread(new Runnable() {
                                        @Override
                                        public void run() {
                                            HttpUtils.doPost("trim/addprocad", trimj, new Callback() {
                                                @Override
                                                public void onFailure(Call call, IOException e) {
                                                    Looper.prepare();
                                                    Toast.makeText(landactivty.this, "网络连接失败", Toast.LENGTH_LONG).show();
                                                    Looper.loop();
                                                }

                                                @Override
                                                public void onResponse(Call call, Response response) throws IOException {
                                                    res = response.body().string();
                                                    Message msg = new Message();
                                                    msg.what =4;
                                                    uiHandler.sendMessage(msg);
                                                }
                                            });
                                        }
                                    });

                                }
                            });
                            buttoncacel.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    dialogtrim.cancel();
                                    onCreate(null);
                                }
                            });

                        } else {
                            Toast.makeText(landactivty.this, "请重新操作", Toast.LENGTH_SHORT).show();
                        }

                    }
                }).setNegativeButton(R.string.cancel, null).show();


            }
        });


        FloatingActionButton floatingActionButton = (FloatingActionButton) findViewById(R.id.landadd);
        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                HttpUtils.doGet("land/addad", new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Toast.makeText(landactivty.this, "网络连接失败", Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String users = response.body().string();
                        userdata = new ArrayList<HashMap<String, Object>>();
                        userList = parseJSONWithGSON(users);
                        for (user u :
                                userList) {
                            HashMap<String, Object> item = new HashMap<String, Object>();
                            item.put("username", u.getUserName());
                            item.put("userid", u.getUserId());
                            //  userdata.add(u.getUserName());
                            Log.i("uses", u.toString());
                            userlist1.add(u.getUserName());
                            userdata.add(item);
                        }
                        landactivty.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {

                                dialog();
                            }
                        });


                    }
                });

               /* Intent i=new Intent();
                i.setClass(landactivty.this,Login.class);
                startActivity(i);*/
            }
        });
    }

    //改变指定position的item背景色为蓝色，即设isfocus为true
    public void setItemBGColorBlue(int position) {
        data.get(position).put("isfocus", true);
        adapter.notifyDataSetChanged();
    }

    //改变指定position的item背景色为白色，即设isfocus为false
    public void setItemBGColorWhite(int position) {
        data.get(position).put("isfocus", false);
        adapter.notifyDataSetChanged();
    }

    private List<user> parseJSONWithGSON(String jsonData) {
        //使用轻量级的Gson解析得到的json
        Gson gson = new Gson();
        List<user> userlist = (ArrayList<user>) gson.fromJson(jsonData, new TypeToken<List<user>>() {
        }.getType());
        return userlist;
    }

    View view1;

    public void dialog() {
        builder = new AlertDialog.Builder(landactivty.this);
        builder.setTitle("添加茶园");
        dialoginit();

        Button buttonok = (Button) view1.findViewById(R.id.landaddok);
        Button buttoncancel = (Button) view1.findViewById(R.id.landaddcancel);

        buttonok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean b = getviewdata();
                if (!b) {
                    return;
                }
                Gson gson = new Gson();
                final String land = gson.toJson(l);
                Log.i("land", land.toString());
                Thread thread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        HttpUtils.doPost("land/addprocad", land, new Callback() {
                            @Override
                            public void onFailure(Call call, IOException e) {
                                Toast.makeText(landactivty.this, "网络连接失败", Toast.LENGTH_LONG).show();

                            }

                            @Override
                            public void onResponse(Call call, Response response) throws IOException {
                                res = response.body().string();
                                Log.i("res", res);
                                Message msg = new Message();
                                msg.what = 2;
                                uiHandler.sendMessage(msg);

                            }
                        });
                    }
                });
                thread.start();

            }
        });
        buttoncancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onCreate(null);
                dialog1.cancel();
                return;
            }
        });


    }

    AlertDialog.Builder builder;

    public void dialoginit() {
        landstatedate = new ArrayList<HashMap<String, Object>>();

        HashMap<String, Object> item = new HashMap<String, Object>();
        item.put("landstateid", 0);
        item.put("landstatename", "关闭");
        landstatedate.add(item);
        HashMap<String, Object> item1 = new HashMap<String, Object>();

        item1.put("landstateid", 1);
        item1.put("landstatename", "正常");
        landstatedate.add(item1);
        dialog1 = builder.create();

        view1 = LayoutInflater.from(landactivty.this).inflate(R.layout.landadd, null);


        Spinner s = (Spinner) view1.findViewById(R.id.userlist);
        Spinner s1 = (Spinner) view1.findViewById(R.id.statelist);

        SimpleAdapter simpleAdapter = new SimpleAdapter(landactivty.this, userdata, R.layout.useritem, new String[]{"username"}, new int[]{R.id.usertext});
        SimpleAdapter simpleAdapter1 = new SimpleAdapter(landactivty.this, landstatedate, R.layout.landstateitem, new String[]{"landstatename"}, new int[]{R.id.landstatetext});

        s.setAdapter(simpleAdapter);
        s1.setAdapter(simpleAdapter1);
        dialog1.setView(view1);

        s.setDropDownVerticalOffset(30);
        s1.setDropDownVerticalOffset(30);


        dialog1.show();


        //获取屏幕的长宽
        WindowManager window = getWindowManager();
        Display display = window.getDefaultDisplay();
        int screenheight = display.getHeight();
        int screenWidth = display.getWidth();

        //设置弹出框的长宽
        dialog1.getWindow().setLayout(screenWidth - 10, screenheight - 150);

        s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Map<String, String> m = (Map<String, String>) parent.getSelectedItem();
                String landuser = m.get("userid");
                l.setLandFunctionary(landuser);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Map<String, Object> m = (Map<String, Object>) parent.getSelectedItem();
                int landstate = Integer.parseInt(m.get("landstateid") + "");

                l.setLandState(landstate);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }


    public void getuselist() {
        HttpUtils.doGet("land/addad", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Toast.makeText(landactivty.this, "网络连接失败", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String users = response.body().string();
                userdata = new ArrayList<HashMap<String, Object>>();
                userList = parseJSONWithGSON(users);
                for (user u :
                        userList) {
                    HashMap<String, Object> item = new HashMap<String, Object>();
                    item.put("username", u.getUserName());
                    item.put("userid", u.getUserId());
                    //  userdata.add(u.getUserName());
                    Log.i("uses", u.toString());
                    userlist1.add(u.getUserName());
                    userdata.add(item);
                }


            }
        });
    }


    public boolean getviewdata() {
        EditText landname1 = (EditText) view1.findViewById(R.id.landname);
        String landname = landname1.getText().toString().trim();
        String landvarite = ((EditText) view1.findViewById(R.id.landvarite)).getText().toString().trim();
        String landtime = ((EditText) view1.findViewById(R.id.landtime)).getText().toString().trim();
        String landsite = ((EditText) view1.findViewById(R.id.landsite)).getText().toString().trim();
        String landarea = ((EditText) view1.findViewById(R.id.landarea)).getText().toString().trim();
        if (landname.length() > 0 && landvarite.length() > 0 && landtime.length() > 0 && landsite.length() > 0 && landarea.length() > 0) {
            l.setLandName(landname);
            l.setLandVariety(landvarite);
            l.setLandSite(landsite);
            Pattern p = Pattern.compile("^[-\\+]?[\\d]*$");
            if (!p.matcher(landtime).matches() || !p.matcher(landarea).matches()) {
                Toast.makeText(landactivty.this, "时间、面积需要输入数字", Toast.LENGTH_LONG).show();
                //dialog.cancel();
                return false;
            }

            l.setLandTime(landtime);
            l.setLandArea(landarea);
            HashMap<String, String> map = new HashMap<String, String>();
            Bean2Map bean2Map = new Bean2Map();
            try {
                map = bean2Map.convertToMap(l);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return true;
        } else {
            Toast.makeText(landactivty.this, "请输入内容", Toast.LENGTH_LONG).show();
            return false;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        onCreate(null);
    }

    private Handler uiHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    Log.i("resres", res);
                    if (res.equals("success")) {
                        Toast.makeText(landactivty.this, "修改成功", Toast.LENGTH_LONG).show();
                        dialog1.cancel();
                    } else if (res.equals("error")) {
                        Toast.makeText(landactivty.this, "存在相同名", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(landactivty.this, "修改失败", Toast.LENGTH_LONG).show();
                    }
                    break;

                case 2:
                    if (res.equals("success")) {

                        Toast.makeText(landactivty.this, "插入成功", Toast.LENGTH_LONG).show();

                        dialog1.cancel();
                        onCreate(null);


                    } else if (res.equals("error")) {

                        Toast.makeText(landactivty.this, "存在相同名的茶园", Toast.LENGTH_LONG).show();

                    } else {

                        Toast.makeText(landactivty.this, "插入失败", Toast.LENGTH_LONG).show();

                        return;
                    }
                    break;
                case 3:
                    if (res.equals("success")) {

                        Toast.makeText(landactivty.this, "插入成功", Toast.LENGTH_LONG).show();

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                dialogm.cancel();
                                onCreate(null);
                            }
                        });
                        onCreate(null);
                        dialogm.cancel();

                    } else {
                        ;
                        Toast.makeText(landactivty.this, "插入失败", Toast.LENGTH_LONG).show();

                        dialogm.cancel();
                        onCreate(null);
                    }
                    break;
                case 4:
                    if (res.equals("success")) {

                        Toast.makeText(landactivty.this, "插入成功", Toast.LENGTH_LONG).show();


                        dialogtrim.cancel();
                        onCreate(null);


                    } else {

                        Toast.makeText(landactivty.this, "插入失败", Toast.LENGTH_LONG).show();

                        dialogtrim.cancel();
                        onCreate(null);
                    }
                    break;
                default:
                    break;
            }

        }
    };
}
